import { service, serviceMST, serviceDYXT } from './service'

import { config } from './config'

const { default_headers } = config

const request = (option: any) => {
  const {
    url,
    method,
    params,
    data,
    headersType,
    responseType
  } = option
  return service({
    url: url,
    method,
    params,
    data,
    responseType: responseType,
    headers: {
      'Content-Type': headersType || default_headers
    }
  })
}
const requestMST = (option: any) => {
  const {
    url,
    method,
    params,
    data,
    headersType,
    responseType
  } = option
  return serviceMST({
    url: url,
    method,
    params,
    data,
    responseType: responseType,
    headers: {
      'Content-Type': headersType || default_headers
    }
  })
}
const requestDYXT = (option: any) => {
  const {
    url,
    method,
    params,
    data,
    headersType,
    responseType
  } = option
  return serviceDYXT({
    url: url,
    method,
    params,
    data,
    responseType: responseType,
    headers: {
      'Content-Type': headersType || default_headers
    }
  })
}
export default {
  get: async <T = any>(option: any) => {
    const res = await request({ method: 'GET', ...option })
    return res?.data as unknown as T
  },
  getBlob: async <T = any>(option: any) => {
    const res = await request({ method: 'GET', ...option })
    return res as unknown as T
  },
  post: async <T = any>(option: any) => {
    const res = await request({ method: 'POST', ...option })
    return res?.data as unknown as T
  },
  postOriginal: async (option: any) => {
    const res = await request({ method: 'POST', ...option })
    return res
  },
  delete: async <T = any>(option: any) => {
    const res = await request({
      method: 'DELETE',
      ...option
    })
    return res?.data as unknown as T
  },
  put: async <T = any>(option: any) => {
    const res = await request({ method: 'PUT', ...option })
    return res?.data as unknown as T
  },
  download: async <T = any>(option: any) => {
    const res = await request({
      method: 'GET',
      responseType: 'blob',
      ...option
    })
    return res as unknown as Promise<T>
  },
  upload: async <T = any>(option: any) => {
    option.headersType = 'multipart/form-data'
    const res = await request({ method: 'POST', ...option })
    return res as unknown as Promise<T>
  },
  // 煤市通
  getMST: async <T = any>(option: any) => {
    const res = await requestMST({
      method: 'GET',
      ...option
    })
    return res?.data as unknown as T
  },
  getBlobMST: async <T = any>(option: any) => {
    const res = await requestMST({
      method: 'GET',
      ...option
    })
    return res as unknown as T
  },
  postMST: async <T = any>(option: any) => {
    const res = await requestMST({
      method: 'POST',
      ...option
    })
    return res?.data as unknown as T
  },
  postOriginalMST: async (option: any) => {
    const res = await requestMST({
      method: 'POST',
      ...option
    })
    return res
  },
  deleteMST: async <T = any>(option: any) => {
    const res = await requestMST({
      method: 'DELETE',
      ...option
    })
    return res?.data as unknown as T
  },
  putMST: async <T = any>(option: any) => {
    const res = await requestMST({
      method: 'PUT',
      ...option
    })
    return res?.data as unknown as T
  },
  downloadMST: async <T = any>(option: any) => {
    const res = await requestMST({
      method: 'GET',
      responseType: 'blob',
      ...option
    })
    return res as unknown as Promise<T>
  },
  uploadMST: async <T = any>(option: any) => {
    option.headersType = 'multipart/form-data'
    const res = await requestMST({
      method: 'POST',
      ...option
    })
    return res as unknown as Promise<T>
  },
  // 调运系统
  getDYXT: async <T = any>(option: any) => {
    const res = await requestDYXT({
      method: 'GET',
      ...option
    })
    return res?.data as unknown as T
  },
  getBlobDYXT: async <T = any>(option: any) => {
    const res = await requestDYXT({
      method: 'GET',
      ...option
    })
    return res as unknown as T
  },
  postDYXT: async <T = any>(option: any) => {
    const res = await requestDYXT({
      method: 'POST',
      ...option
    })
    return res?.data as unknown as T
  },
  postOriginalDYXT: async (option: any) => {
    const res = await requestDYXT({
      method: 'POST',
      ...option
    })
    return res
  },
  deleteDYXT: async <T = any>(option: any) => {
    const res = await requestDYXT({
      method: 'DELETE',
      ...option
    })
    return res?.data as unknown as T
  },
  putDYXT: async <T = any>(option: any) => {
    const res = await requestDYXT({
      method: 'PUT',
      ...option
    })
    return res?.data as unknown as T
  },
  downloadDYXT: async <T = any>(option: any) => {
    const res = await requestDYXT({
      method: 'GET',
      responseType: 'blob',
      ...option
    })
    return res as unknown as Promise<T>
  },
  uploadDYXT: async <T = any>(option: any) => {
    option.headersType = 'multipart/form-data'
    const res = await requestDYXT({
      method: 'POST',
      ...option
    })
    return res as unknown as Promise<T>
  }
}
